package com.gopay.bis.web.controller.ebank;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.CmbcConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcB2bMessage;

@Controller
@Scope("prototype")
public class CmbcB2bServerRes extends BankResBase {
	
	private static Log logger = LogFactory.getLog(CmbcB2bServerRes.class);

	@RequestMapping(value = "/cmbcB2bServerRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- CmbcB2bServerRes bankback ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		CmbcB2bMessage message = new CmbcB2bMessage();
		message.setBank(BankConstants.BANK_LOGO_CMBC);
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));
	        String line = null;  
	        StringBuilder sb = new StringBuilder();  
	        while((line = br.readLine())!=null){  
	            sb.append(line);  
	        }
	        String signText = sb.toString();
			message.setSignMessage(signText);
			//银行业务产品01企业网银支付
			message.setBankBusProd(BisBankBusinessProd._01);
			// 银行接口通道信息 00: 直连
	        message.setBankIntrChann(BisBankInterfaceChannel._00);
		} catch (IOException e) {
			logger.error("CMBC error"+e.getMessage());
		} finally {
			try {
				if (null != br) {
                    br.close();
                }
			} catch (IOException e) {
				logger.error("关闭IO流异常： ", e);
			}
		}
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		CmbcB2bMessage message = (CmbcB2bMessage) result.getModel("res");
		OrderInfo orderInfo = new OrderInfo();
		if (CmbcConstants.ORDER_STATE_SUCCESS.equals(message.getOrderStatus())) {
			orderInfo.setOrderId(message.getOrderId());
			orderInfo.setAmt(message.getAmount().toString());
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("00");
			orderInfo.setRetCode("00");
			orderInfo.setRetMsg("");
		} else {
			orderInfo.setOrderId(message.getOrderId());
			orderInfo.setAmt(message.getAmount().toString());
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("01");
			orderInfo.setRetCode("01");
			orderInfo.setRetMsg(request.getParameter("resMsg"));
		}
		return orderInfo;
	}


	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		return true;
	}
}
